# Contributor: Zach DeCook <zachdecook@librem.one>
# Maintainer: Zach DeCook <zachdecook@librem.one>
pkgname=manticoresearch
pkgver=5.0.2
pkgrel=11
pkgdesc="Easy to use open source fast database for search"
url="https://manticoresearch.com/"
# 32-bit systems fail to build, tests fail on s390x, ppc64le doc fail.
arch="all !ppc64le !s390x !x86 !armhf !armv7"
license="GPL-2.0-only"
install="$pkgname.pre-install"
source="$pkgname-$pkgver.tar.gz::https://github.com/manticoresoftware/manticoresearch/archive/refs/tags/$pkgver.tar.gz
	0001-Documentation-Create-go-md2man-pages-for-indexer.1-i.patch
	system-deps.patch
	pointer-null.patch
	$pkgname.initd
	"
makedepends="
	bison
	boost-dev
	cmake
	expat-dev
	flex
	go-md2man
	gtest-dev
	icu-dev
	libpq-dev
	libstemmer-dev
	mariadb-connector-c-dev
	re2-dev
	samurai
	unixodbc-dev
	zstd-dev
	"
checkdepends="php82"
pkgusers="manticore"
pkggroups="manticore"
subpackages="
	$pkgname-converter
	$pkgname-dev
	$pkgname-doc
	$pkgname-icudata
	$pkgname-openrc
	$pkgname-stopwords
	$pkgname-tools
	"

build() {
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_SYSCONFDIR=/etc \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_INSTALL_LOCALSTATEDIR=/var \
		-DCMAKE_INSTALL_FULL_LOCALSTATEDIR=/var \
		-DCMAKE_BUILD_TYPE=Release \
		-DWITH_GALERA=0
	cmake --build build
}

check() {
	cd build
	CTEST_OUTPUT_ON_FAILURE=TRUE ctest -j1
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	# User/Group created by pre-install hook.
	chown -R $pkgusers:$pkggroups "$pkgdir"/var/lib/manticore
	chown -R $pkgusers:$pkggroups "$pkgdir"/var/log/manticore
	chown -R $pkgusers:$pkggroups "$pkgdir"/var/run/manticore
}

_md_cleanup() {
	# Remove HTML comments and markdown links.
	sed 's/<!--.\+\?-->//g' | sed 's/\[\([^]]*\)\]([^)]*)/\1/g'
}

doc() {
	cd "$builddir/manual/"
	mkdir -p man
	# Documents manually patched to contain man pages.
	cat Starting_the_server/Manually.md \
		Miscellaneous_tools.md \
		Installation/Migration_from_Sphinx.md \
		Adding_data_from_external_storages/Plain_indexes_creation.md \
	| awk 'BEGIN{of="/dev/null"} /^# [a-z-]* [1457]/{of="man/"$2"."$3".md"} (1){print $0 > of}'

	mkdir -p "$subpkgdir"/usr/share/man/man1 "$subpkgdir"/usr/share/man/man5
	for i in man/*.1.md man/*.5.md; do
		local name="$(echo "$i" | cut -d/ -f2)"
		local num="$(echo "$name" | cut -d. -f2)"
		_md_cleanup < "$i" | go-md2man > "$subpkgdir/usr/share/man/man$num/$name"
	done

	# Let default_doc compress man files.
	default_doc
}

converter() {
	pkgdesc="Update Sphinx/Manticore 2.X indexes to new format for Manticore Search"
	amove usr/bin/index_converter
}

tools() {
	pkgdesc="indexer, index-tool, and miscellaneous tools for Manticore Search"
	mkdir -p "$subpkgdir/usr/bin"
	amove usr/bin/indexer \
		usr/bin/indextool \
		usr/bin/spelldump \
		usr/bin/wordbreaker
}

icudata() {
	pkgdesc="$pkgdesc (icudata)"
	amove usr/share/manticore/icu
}

stopwords() {
	pkgdesc="$pkgdesc (stopwords)"
	amove usr/share/manticore/stopwords
}

sha512sums="
e16b2f58ef744dcdb22c20119fd9d3bf92da30bd58f3c11261ceedd213ebd696c23e2eb8b4e5e7825f93bd8a529feb492c053b366f4e9a01bd3bb4bfdc513850  manticoresearch-5.0.2.tar.gz
f60fd6fc30ad5752a3618ba29db29bcf20516a622ae0e27bfa07a317e8fe2c45218580183cdff646622db0b77010cf4a658ba3e2a648862abf519efde5a61c67  0001-Documentation-Create-go-md2man-pages-for-indexer.1-i.patch
b87a9155b07f6bc29d3558d267aa3c2e1c9dfb30e22a3f98daeefa4ad656d25519e8c277f765df7cc1b40c090741eb240ca9e18aed4b16a7a71c866142bd05ab  system-deps.patch
adccaea3ca998f5b8d1f31509bb179f15538164bd10280acfca08f56e68d81a93c18d2260a9b046b6ebab24e7bd86f2a4bfbd0317dd0bd8350728de682d69036  pointer-null.patch
d7aec4e7cd4eebaae190083e79bd20c3cc486aaba7557eb846bf9383a21eb6f8d9c30923b187c8ec4062ee5299a432ccc3839e6ed1402e3adc13588cb6525ce9  manticoresearch.initd
"
